package com.weilx959.spt.core.component;

import com.jfoenix.validation.base.ValidatorBase;
import org.apache.commons.collections4.CollectionUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

public abstract class ValidatorFormItemControlBuilder<T, S extends ValidatorFormItemControl<T>, R extends ValidatorFormItemControlBuilder<T, S, R>>
    extends ParentFormItemControlBuilder<T, S, R> {
    protected List<ValidatorBase> validators;


    public R validators(ValidatorBase... validators) {
        if (Objects.isNull(validators)) {
            this.validators = new ArrayList<>();
        }
        Collections.addAll(this.validators, validators);
        return self();
    }

    public R validator(ValidatorBase validator) {
        if (Objects.isNull(validators)) {
            validators = new ArrayList<>();
        }
        validators.add(validator);
        return self();
    }

    @Override
    public S build() {
        S control = super.build();

        if (CollectionUtils.isNotEmpty(validators)) {
            control.addValidator(validators.toArray(new ValidatorBase[0]));
        }
        return control;
    }
}
